﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace XChip_Common
{
    public class GenerateFileHelp
    {
        /// <summary>
        /// 更具文件写入内容
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        public static void StreamFileCreateText(string fileName, string content)
        {
            using (StreamWriter sw = File.CreateText(fileName))
            {
                sw.WriteLine(content);
                sw.Flush();
                sw.Close();
            }
        }



        /// <summary>
        /// 自动关闭进程
        /// </summary>

        public static void Close_Process_ByAll()
        {

            Process[] myprocess = Process.GetProcesses();
            foreach (Process p in myprocess)
            {
                //此处需要手动添加app.config 文件下名称
                if (  p.ProcessName.Contains("cmd")
                    || p.ProcessName.Contains("conhost")
                    || p.ProcessName.Contains("Windows Command Processor"))
                {
                    //p.CloseMainWindow();
                    p.Close();
                    // p.Kill();
                }
            }

        }


        /// <summary>
        /// 根据文件夹路劲下获取文件时间和路径
        /// </summary>
        /// <param name="path">文件夹路劲</param>
        /// <param name="searchPattern">*.txt</param>
        /// <returns>vale时间，key路径</returns>
        public static Dictionary<string, DateTime> GetFileTimePath(string path, string searchPattern)
        {
            string[] filePathArr = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly);
            Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();
            for (int i = 0; i < filePathArr.Length; i++)
            {
                FileInfo fi = new FileInfo(filePathArr[i]);
                fileCreateDate[filePathArr[i]] = fi.CreationTime;
            }
            fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
            return fileCreateDate;
        }

    }
}
